package com.fish.yssb.ui.page.activity.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsoluteLayout;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.UserInfoTable;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.activity.setPara.SetParaOneActivity;
import com.fish.yssb.ui.page.widget.CustomDialog;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;

import io.realm.RealmResults;


/**
 * A login screen that offers login via email/password.
 */
public class AddUserActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = AddUserActivity.class.getSimpleName();

    LinearLayout tv_back;
    EditText et_username;
    EditText et_password;
    TextView et_usertype;
    TextView  btn_login;


    public static void start(Context context) {
        Intent intent = new Intent(context, AddUserActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        PreventKeyboardBlockUtil.getInstance(this).setBtnView(et_password).register();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        PreventKeyboardBlockUtil.getInstance(this).unRegister();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setLightMode(this);
        setContentView(R.layout.activity_add_user);

        initview();

    }


    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_pwd);
        et_usertype = findViewById(R.id.et_usertype);
        btn_login = findViewById(R.id.btn_login);

        tv_back = findViewById(R.id.tv_back);
        btn_login.setOnClickListener(this);
        tv_back.setOnClickListener(this);

        et_usertype.setOnClickListener(this);
        et_usertype.setText("操作员");
    }
    private  CustomDialog choosePicDialog;
    private LinearLayout lyDlg;
    private TextView btn1, btn2,btn3, btnCancel;
    private Window window;
    @Override
    public void onClick(View view) {

        String phone = "";
        String code = "";
        String pwd = "";
        switch (view.getId()) {
            case R.id.btn_login:
                //返回
//                MainActivity.start(this);
//                finish();
                phone = et_username.getText().toString();
                pwd = et_password.getText().toString();
                if ("".equalsIgnoreCase(phone)) {
                    showToast("用户名不能为空");
                    return;
                }
                if ("".equalsIgnoreCase(pwd)) {
                    showToast("密码不能为空");
                    return;
                }


                RealmResults<UserInfoTable> selInfo  = RealmDb.getInstance().selUserInfo(phone);

                if(!selInfo.isEmpty())
                {
                    showToast("用户名已存在，请确认！");
                    return;
                }
                UserInfoTable info = new UserInfoTable();
                info.setName(phone);
                info.setPwd(pwd);
                info.setType(et_usertype.getText().toString());
                ArrayList<UserInfoTable> list = new ArrayList<>();
                list.add(info);
                RealmDb.getInstance().insertRealmObjects(list);

                showToast("新建用户成功");

                finish();



                break;

            case R.id.tv_back:
                finish();
                break;
            case R.id.et_usertype:

                choosePicDialog = new CustomDialog(AddUserActivity.this, true);
                choosePicDialog.setCustomView(R.layout.pic_select_dlg);

                window = choosePicDialog.getDialog().getWindow();
                window.setGravity(Gravity.BOTTOM);
                window.setLayout(AbsoluteLayout.LayoutParams.MATCH_PARENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT);

                lyDlg = (LinearLayout) choosePicDialog.findViewById(R.id.dialog_layout);
                lyDlg.setPadding(0, 0, 0, 0);

                btn1 = (TextView) choosePicDialog.findViewById(R.id.btn1);
                btn2 = (TextView) choosePicDialog.findViewById(R.id.btn2);
                btn3 = (TextView) choosePicDialog.findViewById(R.id.btn3);
                btn1.setText("管理员");
                btn2.setText("操作员");
                btn3.setText("游客");
                btn1.setVisibility(View.VISIBLE);
                btn2.setVisibility(View.VISIBLE);
                btn3.setVisibility(View.VISIBLE);
                btnCancel = (TextView) choosePicDialog.findViewById(R.id.btnCancel);

                btn1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        choosePicDialog.dismiss();

                        et_usertype.setText("管理员");
//                        Toast.makeText(SetParaOneActivity.this,"选择1",Toast.LENGTH_SHORT).show();
                    }
                });

                btn2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        choosePicDialog.dismiss();
                        et_usertype.setText("操作员");
//                        Toast.makeText(SetParaOneActivity.this,"选择2" ,Toast.LENGTH_SHORT).show();
                    }
                });
                btn3.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        choosePicDialog.dismiss();
                        et_usertype.setText("游客");
//                        Toast.makeText(SetParaOneActivity.this,"选择2" ,Toast.LENGTH_SHORT).show();
                    }
                });
                btnCancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        choosePicDialog.dismiss();
                    }
                });

                choosePicDialog.setCancelable(true);
                choosePicDialog.show();

                break;
            default:
                break;
        }


    }


}

